﻿using Anley.DomainDrivenDesign.Contract.Extension;

namespace Anley.DomainDrivenDesign.Contract.Configuration;

/// <summary>
/// 配置扩展类
/// </summary>
public static class ConfigurationExtension
{
    private static IConfiguration? _configuration;

    /// <summary>
    /// 配置
    /// </summary>
    public static IConfiguration Configuration =>
        _configuration.NotNull(nameof(_configuration));

    /// <summary>
    /// 注册配置
    /// </summary>
    /// <param name="services"></param>
    public static IConfiguration RegisterConfiguration(
        this IServiceCollection services)
    {
        // 注入IConfiguration
        _configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .Build();
        services.AddSingleton(_configuration);
        // 注入自定义配置
        services.Configure<AuthorizationConfiguration>(_configuration.GetSection("Authorization"));
        services.Configure<EncryptionConfiguration>(_configuration.GetSection("Encryption"));
        services.Configure<SmtpConfiguration>(_configuration.GetSection("Smtp"));
        // 返回配置
        return _configuration;
    }
}